package com.tourair.action.admin;

import java.util.Date;

import net.jdiy.core.Args;
import net.jdiy.core.Ls;
import net.jdiy.core.Rs;

import org.apache.commons.lang.StringUtils;

import com.tourair.action.BaseAction;
import com.tourair.common.Constant;
import com.tourair.common.Message;

/**
 * CRUD Demo
 * 
 * @author Tam
 * 
 */
public class DemoAction extends BaseAction {

	// 表名
	private final String TABLE_NAME = "ta_demo";

	/**
	 * 添加|修改功能 录入页
	 * 
	 * @throws Exception
	 */
	public void add() throws Exception {
		forward("/WEB-INF/admin/demo/add.jsp");
	}

	/**
	 * 查询列表
	 * 
	 * @throws Exception
	 */
	public void list() throws Exception {
		try {
			String whereCondition = " 1=1 AND status <> '" + Constant.STATUS_DELETE + "'";

			String name = app.get("name");
			if (StringUtils.isNotBlank(name)) {
				whereCondition += " AND name like '%" + name + "%' ";
			}

			String beginDate = app.get("beginDate");
			if (StringUtils.isNotBlank(beginDate)) {
				whereCondition += " AND create_time  >= '" + beginDate + " 00:00:00'";
			}

			String endDate = app.get("endDate");
			if (StringUtils.isNotBlank(endDate)) {
				whereCondition += " AND create_time  <= '" + endDate + " 23:59:59'";
			}

			String orderByCondition = "";
			if (StringUtils.isNotBlank(orderField)) {
				orderByCondition += " ORDER BY " + orderField;
				if (StringUtils.isNotBlank(orderDirection)) {
					orderByCondition += " " + orderDirection;
				}
			} else {
				orderByCondition += " ORDER BY create_time DESC";
			}
			Ls ls = dao.ls(new Args(TABLE_NAME, whereCondition + orderByCondition, numPerPage, pageNumber));

			setAttr("ls", ls);
			setAttr("orderField", orderField);
			setAttr("orderDirection", orderDirection);

			forward("/WEB-INF/admin/demo/list.jsp");
		} catch (Exception e) {
			log.error("查询列表出错" + e.toString());
		}
	}

	/**
	 * 保存|修改
	 * 
	 * @throws Exception
	 */
	public String saveorupdate() throws Exception {
		try {
			// 从页面获取from清单，组装成Rs对象
			Rs rs = getRs(TABLE_NAME, "rs");

			// 查询用户是否存在
			Rs demo = dao.rs(new Args(TABLE_NAME, " name='" + rs.get("name") + "'"));

			// StringUtils.isBlank(rs.get("id"))为true表示为添加情况，修改时rs.get("id")会有值（其实就是判断表记录的主键）
			if (!demo.isNull() && StringUtils.isBlank(rs.get("id")) && StringUtils.isNotBlank(demo.get("id"))) {
				// 转发到错误页面
				forwardError("对不起，用户已经存在！");
				return null;
			} else {
				if (StringUtils.isBlank(rs.get("id"))) {
					rs.set("create_time", new Date());
				}
				dao.save(rs);
				// 重定向到list页面
				redirect("admin_Demo_list.action");
				return null;
			}
		} catch (Exception e) {
			log.error("添加信息失败 " + e.toString());
			// 转发到错误页面
			forwardError("对不起，添加出错!<br/>" + e.getMessage());
		}
		return null;
	}

	/**
	 * 删除
	 * 
	 * @throws Exception
	 */
	public void delete() throws Exception {
		Message msg = null;
		try {
			String ids = app.get("ids");
			if (StringUtils.isNotBlank(ids)) {
				int count = dao.exec("UPDATE " + TABLE_NAME + " SET status = '" + Constant.STATUS_DELETE + "'  WHERE id IN(" + app.get("ids") + ")");
				if (count > 0) {
					msg = new Message("删除成功", Constant.MESSAGE_SUCCESS);
				}else{
					msg = new Message("删除失败", Constant.MESSAGE_ERROR);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = new Message("删除失败", Constant.MESSAGE_ERROR);
		}
		renderJson(msg);
	}

}
